import searchHtml from '../view/search.html';
import fetch from '../model/fetch';
import position from '../view/position.html';
import city from '../view/city.html';

export default class SearchController {
    constructor() {
        this.pageNo = 1;
        this.pageSize = 15;
    }
    render() {
        var tmparr = JSON.parse(localStorage.getItem("lagou.com"));
        console.log(tmparr)
        if (localStorage.getItem("lagou.com")) {
            var tmphtml = template.render(searchHtml, { his: tmparr });
            $(".swiper-slide").eq(1).html(tmphtml);
        } else {
            $(".swiper-slide").eq(1).html(searchHtml);
            $("#history").hide();
        }

        this.initEvent();
    }
    initEvent() {
        var self = this;
        //点击搜索按钮
        $("#searchbtn").on('click', async function () {
            var scontent = $("#scontent").val();
            var place = $(".city").text();
            let datas = await new fetch().getPositionlist({
                pageNo: self.pageNo,
                pageSize: self.pageSize
            });
            let positionArr = datas.content.data.page.result;
            let fitResults = [];
            $("#history").hide();
            // 搜索结果存入数组，等待渲染
            for (let i = 0; i < positionArr.length; i++) {
                if (positionArr[i].city == place && positionArr[i].positionName.indexOf(scontent) != -1) {
                    fitResults.push(positionArr[i]);
                }
            }
            if (fitResults.length == 0) {
                $(".tip").show();
            } else {
                $(".tip").hide();
                //搜索到的数据渲染到页面
                var fitHtml = template.render(position, { data: fitResults });
                $(".search-result").html(fitHtml);
            }

            //判断localstorage有没有数据，没有的话就创建一个数组，有的话将原来的先赋值给新数组
            var flag = false;
            if (!localStorage.getItem('lagou.com')) {
                var historyInput = [];
            } else {
                historyInput = JSON.parse(localStorage.getItem('lagou.com'));
            }
            //搜索历史存入localstorage
            for (let i = 0; i < historyInput.length; i++) {
                if (historyInput[i].text == scontent) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                var tmp = {
                    text: scontent,
                    time: new Date().getTime()
                }
                historyInput.push(tmp);
                localStorage.setItem('lagou.com', JSON.stringify(historyInput));
            }
        });
        //删除历史记录
        $(".delcon").on('click', function () {
            var index;
            var deltxt = $(this).prev("span").text();
            var arr = JSON.parse(localStorage.getItem('lagou.com'));
            for (let i = 0; i < arr.length; i++) {
                if (arr[i].text == deltxt) {
                    arr.splice(i, 1);
                    break;
                }
            }
            localStorage.setItem("lagou.com", JSON.stringify(arr));
            $(this).parent().remove();

        })
        //点击城市，展示城市列表页
        $(".sbutton").on('click', function () {
            $("#searchDiv").hide();
            $(".fdialog-content").show();
        });
        //选中城市，返回显示
        $(".main_global").on('click', 'td', function () {
            $("#searchDiv").show();
            $(".fdialog-content").hide();
            $(".city").html($(this).text());
        })
        //输入框为空即显示历史记录
        $("#scontent").on('input', function () {
            if ($(this).val() == '') {
                $("#history").show();
                $(".search-result").hide();
                $(".tip").hide();
            }
        })
        //点击选中的历史记录,输入框自动出现文字
        $(".histxt").on('click', function () {
            $("#scontent").val($(this).text());
        })
    }
}